﻿using MassTransit;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
namespace Athena.Observer
{
    public class DefaultBusObserver : BaseObserver<DefaultBusObserver>, IBusObserver
    {

        public DefaultBusObserver(ILogger logger) : base(logger)
        {

        }

        public virtual Task CreateFaulted(Exception exception)
        {
            logger.LogError($"Bus创建失败.\r\n 错误内容:{exception.Message}.\r\n 堆栈跟踪:{exception.StackTrace}");
            return Task.CompletedTask;
        }

        public virtual Task PostCreate(IBus bus)
        {
            logger.LogInformation($"Bus[{bus.Address.ToString()}]已创建成功。Bus可以准备执行Start操作");
            return Task.CompletedTask;
        }

        public virtual Task PostStart(IBus bus, Task<BusReady> busReady)
        {
            logger.LogInformation($"Bus[{bus.Address.ToString()}]已Start成功。准备执行任务中...");
            return Task.CompletedTask;
        }

        public virtual Task PostStop(IBus bus)
        {
            logger.LogInformation($"Bus[{bus.Address.ToString()}]已Stop成功。");
            return Task.CompletedTask;
        }

        public virtual Task PreStart(IBus bus)
        {
            logger.LogInformation($"Bus[{bus.Address.ToString()}]准备执行Start任务。");
            return Task.CompletedTask;
        }

        public virtual Task PreStop(IBus bus)
        {
            logger.LogInformation($"Bus[{bus.Address.ToString()}]准备执行Stop任务。");
            return Task.CompletedTask;
        }

        public virtual Task StartFaulted(IBus bus, Exception exception)
        {
            logger.LogError($"Bus[{bus.Address.ToString()}]执行Start任务失败.\r\n 错误内容:{exception.Message}.\r\n 堆栈跟踪:{exception.StackTrace}");
            return Task.CompletedTask;
        }

        public virtual Task StopFaulted(IBus bus, Exception exception)
        {
            logger.LogError($"Bus[{bus.Address.ToString()}]执行Stop任务失败.\rn 错误内容:{exception.Message}.\r\n 堆栈跟踪:{exception.StackTrace}");
            return Task.CompletedTask;
        }
    }
}
